/**
 * 商家服务类
 * 用于获取商家列表、商家详情等商家相关操作
 */

const request = require('../libs/request');

class BizService {
	/**
	 * 获取商家列表（含分页）
	 * @param {Object} params 查询参数
	 * @param {number} params.page 页码，默认1
	 * @param {number} params.pageSize 每页数量，默认10
	 * @param {string} params.keyword 搜索关键词，可选
	 * @param {string} params.type 筛选类型，默认recommend
	 * @param {string} params.sortType 排序类型：default-默认, distance-距离, rating-好评, star-星级
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回商家列表数据
	 */
	static async getBizList(params = {}) {
		const { 
			page = 1, 
			pageSize = 10, 
			keyword = '', 
			type='recommend',
			range='recommend',
			sortType = 'default',
			showLoading = false 
		} = params;

		try {
			const response = await request.get('/v1/biz/index', {
				showLoading,
				needAuth: false,
				data: {
					page,
					page_size: pageSize,
					keyword,
					type,
					range,
					sort_type: sortType
				}
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '获取商家列表成功'
			};
		} catch (error) {
			console.error('获取商家列表失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取商家列表失败'
			};
		}
	}

	/**
	 * 搜索商家
	 * @param {Object} params 查询参数
	 * @param {number} params.page 页码，默认1
	 * @param {number} params.pageSize 每页数量，默认10
	 * @param {string} params.keyword 搜索关键词
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回搜索结果数据
	 */
	static async searchBiz(params = {}) {
		const { 
			page = 1, 
			pageSize = 10, 
			keyword = '', 
			showLoading = false 
		} = params;

		if (!keyword.trim()) {
			return {
				success: false,
				data: null,
				message: '搜索关键词不能为空'
			};
		}

		try {
			const response = await request.get('/v1/biz/search', {
				showLoading,
				needAuth: false,
				data: {
					current: page,
					pageSize,
					keyword: keyword.trim()
				}
			});

			return {
				success: true,
				data: response.data,
				total: response.total,
				message: response.message || '搜索成功'
			};
		} catch (error) {
			console.error('搜索商家失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '搜索商家失败'
			};
		}
	}

	/**
	 * 获取商家详情
	 * @param {Object} params 查询参数
	 * @param {number} params.id 商家ID
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回商家详情数据
	 */
	static async getBizDetail(params = {}) {
		const { id, showLoading = false } = params;

		if (!id) {
			return {
				success: false,
				data: null,
				message: '商家ID不能为空'
			};
		}

		try {
			const response = await request.get(`/v1/biz/detail`, {
				showLoading,
				needAuth: false,
                data: {
                    id
                }
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '获取商家详情成功'
			};
		} catch (error) {
			console.error('获取商家详情失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取商家详情失败'
			};
		}
	}

	/**
	 * 获取商家商品列表
	 * @param {Object} params 查询参数
	 * @param {number} params.id 商家ID
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回商品列表数据
	 */
	static async getBizGoods(params = {}) {
		const { id, showLoading = false } = params;

		if (!id) {
			return {
				success: false,
				data: null,
				message: '商家ID不能为空'
			};
		}

		try {
			const response = await request.get(`/v1/biz-goods/index`, {
				showLoading,
				needAuth: false,
				data: {
					id
				}
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '获取商家商品列表成功'
			};
		} catch (error) {
			console.error('获取商家商品列表失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取商家商品列表失败'
			};
		}
	}

	static async bizPings(params = {}) {
		const { id, showLoading = false } = params;
		try {
			const response = await request.get(`/v1/biz/pings`, {
				showLoading,
				needAuth: false,
				data: {
					id
				}
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '获取商家商品列表成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取商家商品列表失败'
			};
		}
	}

	// 扫码获得一次支付记录
	static async payQrcodeRecord(params = {}) {
		const { id, showLoading = false } = params;
		try {
			const response = await request.post(`/v1/biz/pay-qrcode-record`, {
				id,
				needAuth: true,
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '获取商家商品列表成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取商家商品列表失败'
			};
		}
	}

	/**
	 * 获取结算单列表
	 * @param {Object} params 查询参数
	 * @param {number} params.page 页码，默认1
	 * @param {number} params.pageSize 每页数量，默认10
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回结算单列表数据
	 */
	static async getBillList(params = {}) {
		const { 
			page = 1, 
			pageSize = 10, 
			showLoading = false 
		} = params;

		try {
			const response = await request.get('/v1/bill/index', {
				showLoading,
				needAuth: true,
				data: {
					page,
					pageSize
				}
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '获取结算单列表成功'
			};
		} catch (error) {
			console.error('获取结算单列表失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取结算单列表失败'
			};
		}
	}

	/**
	 * 获取结算单详情
	 * @param {Object} params 查询参数
	 * @param {number} params.id 结算单ID
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回结算单详情数据
	 */
	static async getBillDetail(params = {}) {
		const { id, showLoading = false } = params;

		if (!id) {
			return {
				success: false,
				data: null,
				message: '结算单ID不能为空'
			};
		}

		try {
			const response = await request.get('/v1/bill/detail', {
				showLoading,
				needAuth: true,
				data: {
					id
				}
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '获取结算单详情成功'
			};
		} catch (error) {
			console.error('获取结算单详情失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取结算单详情失败'
			};
		}
	}

	/**
	 * 商家确认账单
	 */
	static async confirmBill(params = {}) {
		const { id, showLoading = false } = params;
		try {
			const response = await request.post('/v1/bill/confirm', {
				showLoading,
				needAuth: true,
				data: {
					id
				}
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '确认结算单成功'
			};
		} catch (error) {
			console.error('确认结算单失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '确认结算单失败'
			};
		}
	}

	/**
	 * 获取商家管理员列表
	 * @param {Object} params 查询参数
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回管理员列表数据
	 */
	static async getBizAdmins(params = {}) {
		const { showLoading = false } = params;

		try {
			const response = await request.get('/v1/biz/get-admins', {
				showLoading,
				needAuth: true
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '获取管理员列表成功'
			};
		} catch (error) {
			console.error('获取管理员列表失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取管理员列表失败'
			};
		}
	}

	/**
	 * 添加商家管理员
	 * @param {Object} params 查询参数
	 * @param {string} params.phone 手机号
	 * @param {string} params.role 角色（super/normal）
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回添加结果
	 */
	static async addBizAdmin(params = {}) {
		const { phone, role = 'normal', showLoading = false } = params;

		if (!phone) {
			return {
				success: false,
				data: null,
				message: '手机号不能为空'
			};
		}

		if (!['super', 'normal'].includes(role)) {
			return {
				success: false,
				data: null,
				message: '角色参数错误'
			};
		}

		try {
			const response = await request.post('/v1/biz/add-admin', {
				showLoading,
				needAuth: true,
				data: {
					phone,
					role
				}
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '添加管理员成功'
			};
		} catch (error) {
			console.error('添加管理员失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '添加管理员失败'
			};
		}
	}

	/**
	 * 删除商家管理员
	 * @param {Object} params 查询参数
	 * @param {number} params.adminId 管理员ID
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回删除结果
	 */
	static async removeBizAdmin(params = {}) {
		const { adminId, showLoading = false } = params;

		if (!adminId) {
			return {
				success: false,
				data: null,
				message: '管理员ID不能为空'
			};
		}

		try {
			const response = await request.post('/v1/biz/remove-admin', {
				showLoading,
				needAuth: true,
				data: {
					admin_id: adminId
				}
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '删除管理员成功'
			};
		} catch (error) {
			console.error('删除管理员失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '删除管理员失败'
			};
		}
	}

	/**
	 * 获取当前用户的管理员信息
	 * @param {Object} params 查询参数
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回管理员信息
	 */
	static async getCurrentAdminInfo(params = {}) {
		const { showLoading = false } = params;

		try {
			const response = await request.get('/v1/biz/get-current-admin-info', {
				showLoading,
				needAuth: true
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '获取管理员信息成功'
			};
		} catch (error) {
			console.error('获取管理员信息失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取管理员信息失败'
			};
		}
	}
}

module.exports = BizService;
